package 力扣日常刷题.木23一月.第12天312;

import 力扣日常刷题.ListNode;

import java.util.List;

/**
 * @author 帅小伙
 * @date 2022/3/12
 * @description
 */
public class Demo50合并k个升序链表 {

    public ListNode mergeKLists(ListNode[] lists) {
        ListNode ans = null;
        for (int i = 0; i < lists.length; i++) {
            ans = mergeTwoLists(ans,lists[i]);
        }
        return ans;
    }

    public ListNode mergeTwoLists(ListNode a,ListNode b) {
        if(a == null || b == null) return a == null ? b : a;
        if(a.val > b.val) {
            b.next = mergeTwoLists(a,b.next);
            return b;
        }else{
            a.next = mergeTwoLists(a.next,b);
            return a;
        }
    }



}
